package com.demo2do.fms.admin.web.controller.data.business;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.report.ExportType;
import com.demo2do.fms.admin.entity.report.data.VehicleReportSearch;
import com.demo2do.fms.admin.entity.search.data.VehicleSearch;
import com.demo2do.fms.admin.service.DataService;
import com.demo2do.fms.admin.utils.FormatUtils;
import com.demo2do.fms.admin.visitor.impl.PersistentVisitor;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.vehicle.source.Vehicle;

/**
 * 查询驳运车
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/vehicles")
@MenuSetting("menu-data")
public class VehicleController {
    
    @Autowired
    private PersistentVisitor persistentVisitor;
    
    @Autowired
    @Qualifier("dataService")
    private DataService<Vehicle> vehicleService;
    
    /**
     * 驳运车列表
     * 
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-vehicle")
    public ModelAndView list(Page page) {
        
        ModelAndView modelAndView = new ModelAndView("data/business/vehicle/vehicle-list");
        
        int totals = vehicleService.count(Vehicle.class);
        if (totals > 0) {
            
            List<Vehicle> vehicles = vehicleService.listByPage(Vehicle.class, page.with(totals));
            
            modelAndView.addObject("vehicles", vehicles);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }

    /**
     * 
     * @param orgIds
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search(@Secure(property = "orgIds") List<Long> orgIds) {
        
        ModelAndView modelAndView = new ModelAndView("data/business/vehicle/vehicle-search");
        
        modelAndView.addObject("orgIds", orgIds);
        
        return modelAndView;
        
    }
    
    /**
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-vehicle")
    public ModelAndView onSearch(VehicleSearch search, HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("data/business/vehicle/vehicle-list");
        
        int totals = vehicleService.count(Vehicle.class, search.getFilter());
        if (totals > 0) {
            
            List<Vehicle> vehicles = vehicleService.search(Vehicle.class, search, page.with(totals));
            
            modelAndView.addObject("vehicles", vehicles);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 输出当前页记录
     * 
     * @param search
     * @param page
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export(VehicleSearch search, Page page) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("data/business/vehicle-export.xlsx"));
     
        // 1. list result
        int totals = vehicleService.count(Vehicle.class, search.getFilter());
        if (totals > 0) {
            List<Vehicle> vehicles = vehicleService.search(Vehicle.class, search, page.with(totals));
            modelAndView.addObject("vehicles", vehicles);
        }
        
        // 2. attribute
        modelAndView.addObject("persist", persistentVisitor);
        modelAndView.addObject("type", ExportType.CURRENT);
        
        // 3. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
       
        return modelAndView;
    }
    
    /**
     * 输出所有记录
     * 
     * @param search
     * @return
     */
    @RequestMapping(value = "/export-all", method = RequestMethod.GET)
    public ModelAndView exportAll(VehicleReportSearch search) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("data/business/vehicle-export.xlsx"));
        
        // 1. list result
        List<Map<String, Object>> results = vehicleService.export(search, search.buildRowMapper());
        modelAndView.addObject("results", results);
        
        // 2. attribute
        modelAndView.addObject("type", ExportType.ALL);
        
        // 3. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
}
